import regEx from "./regEx.js"

var way = {
  /**
	 * 弹窗提示
	 * @param {String} str 提示语句
	 * @param icon  弹窗图标 默认为不显示 success、loading
	 */
  getUrlParam(name, url) {
    var param = url.substr(url.indexOf('?') + 1);
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = param.match(reg);
    if (r != null) return unescape(r[2]);
    return '';
  },
  get_ymd(shijianchuo) {
    function add0(m) {
      return m < 10 ? '0' + m : m
    }
    // shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    return `${y}-${add0(m)}-${add0(d)}`
  },
  // 获取今日的时间
  today() {
    var timeNum = Date.parse(new Date())
	  var startTime	= this.format(timeNum)
    var endTime = this.format(timeNum, true)
    return { startTime, endTime }
  },
  format(shijianchuo, finallyTime) {
    function add0(m) {
      return m < 10 ? '0' + m : m
    }
    // shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    return `${y}-${add0(m)}-${add0(d)} ${finallyTime ? '23' : '00'}:${finallyTime ? '59' : '00'}:${finallyTime ? '59' : '00'}`
  },
  // 获取昨天的时间
  yesterdayTime() {
    var day = new Date();
    day.setTime(day.getTime() - 24 * 60 * 60 * 1000);
    var yesterday = day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate();
    var startTime = this.format(yesterday)
    var endTime = this.format(yesterday, true)
    return {
      startTime,
      endTime
    }
  },
  // 当前日期往前30天的日期 截至到昨日23:59:59
  near30day() {
    var day_30 = new Date().setMonth((new Date().getMonth() - 1)); // 日期是30天前的对应时间戳
    day_30 = new Date(day_30); // 转换成标准时间
    var y = day_30.getFullYear(); // 年
    var m = day_30.getMonth() + 1; // 月
    var d = day_30.getDate(); // 日
    if (m < 10) {
      m = '0' + m;
    }
    if (d < 10) {
      d = '0' + d;
    }
    day_30 = y + '-' + m + '-' + d; // 日期是30天前的对应字符串 xxxx-xx-xx
    day_30 = this.format(day_30)
    var startTime = day_30
    var {
      endTime
    } = this.yesterdayTime()
    return {
      startTime,
      endTime
    }
  },

  // 当前选择往前七天的日期 截至到昨日23:59:59
  sevenDaysTime() {
    var date3 = new Date().getTime() - 9 * 3600 * 1000
    // let date1 = new Date().getTime() - 9 * 3600 * 1000 - 1
    var date2 = date3 - 8 * (3600 * 24 * 1000)
    var startTime = this.format(date2)
	   var { endTime } = this.yesterdayTime()
    return {
      startTime,
      endTime
    }
  },
  /**
	 * 时间问候语
	 * @param param 当前时间，new Date() 格式
	 * @description param 调用 `formatAxis(new Date())` 输出 `上午好`
	 * @returns 返回拼接后的时间字符串
	 */
  formatAxis(param) {
    var hour = new Date(param).getHours();
    if (hour < 6) return '凌晨好';
    else if (hour < 9) return '早上好';
    else if (hour < 12) return '上午好';
    else if (hour < 14) return '中午好';
    else if (hour < 17) return '下午好';
    else if (hour < 19) return '傍晚好';
    else if (hour < 22) return '晚上好';
    else return '夜里好';
  },
  phone_regExVal(value) {
    var text = ""
    if (!value) {
      text = "请填写手机号"
    } else if (value && regEx.phone(value)) {
      text = "请填写正确的手机号"
    } else {
      text = ""
    }
    return text
  },
  specialChars_regExVal(value, name) {
    var text = ''
    if (!value) {
      text = `请填写${name}`
    } else if (value && regEx.special_char(value)) {
      text = `${name}不能包含特殊字符`
    } else {
      text = ""
    }
    return text
  },
  chineseEnglish_regExVal(value, name) {
    var text = ''
    if (!value) {
      text = `请填写${name}`
    } else if (value && regEx.chineseEnglish(value)) {
      text = `${name}只能填写中英文`
    } else {
      text = ""
    }
    return text
  },

  password_regExVal(value, name) {
    var text = ''
    if (!value) {
      text = `请填写密码`
    } else {
      text = ""
    }
    return text
  },
  select_regExVal(value, name) {
    var text = ''
    if (!value) {
      text = `请选择${name}`
    } else {
      text = ""
    }
    return text
  },
  number_regExVal(value, name) {
    var text = ''
    if (!value) {
      text = `请填写${name}`
    } else {
      text = ""
    }
    return text
  },
  toFixedTwo_regExVal(value, name) {
    var text = ''
    if (!value) {
      text = `请填写${name}`
    } else if (value && regEx.toFixedTwo(value)) {
      text = `${name}请保留到两位小数`
    } else {
      text = ""
    }
    return text
  },

  verifyTextColor(val, text = '', color = 'red') {
    // 返回内容，添加颜色
    var v = text.replace(new RegExp(val, 'gi'), `<span style='color: ${color}'>${val}</span>`);
    // console.log('v==========',v);
    // 返回结果
    return v;
  }
}
export default way
